package com.das.mechanic_alone.mvp.view.alone;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.das.mechanic_alone.R;
import com.das.mechanic_alone.mvp.contract.alone.X3AloneServiceContract;
import com.das.mechanic_alone.mvp.presenter.alone.X3AloneServicePresenter;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.adapter.common.X3AloneServiceItemAdapter;
import com.das.mechanic_base.adapter.common.X3HomeSearchAdapter;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.alone.AloneSearchBean;
import com.das.mechanic_base.bean.alone.SearchBean;
import com.das.mechanic_base.bean.alone.ServiceHeaderBean;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.gen.DaoSessionUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3AloneServiceSearchUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.widget.X3BottomAffirmMileDialog;
import com.das.mechanic_base.widget.X3NewBottomMileOrTimeDialog;
import com.das.mechanic_base.widget.X3PointDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.view.View.VISIBLE;

@Route(path = X3AppRouterConstants.Router.ALONE_SEARCH)
public class X3AloneSearchActivity extends X3BaseActivity<X3AloneServicePresenter> implements TextWatcher, X3HomeSearchAdapter.IOnSaveClick, X3AloneServiceContract.View, X3AloneServiceItemAdapter.IOnClickService, TextView.OnEditorActionListener, X3NewBottomMileOrTimeDialog.IOnAffirmSelect {

    @BindView(R2.id.et_search)
    EditText et_search;
    @BindView(R2.id.iv_clean)
    ImageView iv_clean;
    @BindView(R2.id.tv_search)
    TextView tv_search;
    @BindView(R2.id.ll_result)
    RelativeLayout ll_result;
    @BindView(R2.id.rlv_result)
    RecyclerView rlv_result;
    @BindView(R2.id.rlv_service)
    RecyclerView rlv_service;
    @BindView(R2.id.rl_header)
    RelativeLayout rl_header;
    @BindView(R2.id.tv_new)
    TextView tv_new;
    @BindView(R2.id.tv_result)
    TextView tv_result;
    @BindView(R2.id.tv_del)
    TextView tv_del;

    private String mobile;
    private X3HomeSearchAdapter saveAdapter;
    private long workBaseId;
    private List<AloneServiceListBean> newCacheList;
    private List<AloneServiceListBean> cacheList;
    private long questionBaseId;
    private X3AloneServiceItemAdapter itemAdapter;
    private String decTitle;
    private AloneServiceListBean changeServiceBean;
    private ObjectAnimator showSet;
    private ObjectAnimator hideSet;
    private X3PointDialog delDialog;
    private X3NewBottomMileOrTimeDialog mileOrTimeDialog;
    private X3BottomAffirmMileDialog mileDialog;
    private AloneServiceListBean serviceListBean;
    private boolean needUpdateMile;
    private double newPerMiles;
    private double newCurrentMile;
    private long questionOptionId;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_activity_alone_search;
    }

    @Override
    protected void initView() {
        X3StatusBarUtil.darkMode(this);

        rl_header.setPadding(0, X3StatusBarUtil.getStatusBarHeight(this),0,0);

        workBaseId = getIntent().getLongExtra("workBaseId", 0L);

        decTitle = getIntent().getStringExtra("decTitle");

        questionBaseId = getIntent().getLongExtra("questionBaseId", 0L);

        questionOptionId = getIntent().getLongExtra("questionOptionId", 0L);

        needUpdateMile = getIntent().getBooleanExtra("needUpdateMile", false);

        mobile = (String) SpHelper.getData("mobile", "");

        et_search.addTextChangedListener(this);
        et_search.setOnEditorActionListener(this);
        /**
         * 进入弹出软件盘
         */
        etGetFouce();

        rlv_result.setLayoutManager(ChipsLayoutManager.newBuilder(this)
                .setOrientation(ChipsLayoutManager.HORIZONTAL)
                .setMaxViewsInRow(4)
                .build());

        rlv_service.setLayoutManager(new LinearLayoutManager(this));

        saveAdapter = new X3HomeSearchAdapter(this);
        rlv_result.setAdapter(saveAdapter);
        saveAdapter.setiOnSaveClick(this);

        itemAdapter = new X3AloneServiceItemAdapter(this);
        rlv_service.setAdapter(itemAdapter);
        itemAdapter.setiOnClickService(this);

        if (mPresenter != null) {
            mPresenter.getDetectionServiceList(false);
        }

        searchContent();

    }

    /**
     * 搜索本地缓存中添加的服务项目
     */
    @Override
    public void searchAloneCache() {
        if(mPresenter != null){
            SearchBean searchBean = new SearchBean();
            searchBean.keyword = et_search.getText().toString().trim();
            searchBean.workBaseId = workBaseId;
            mPresenter.listServiceBaseByCategory(searchBean);
        }
    }

    @Override
    public void listServiceBaseByCategorySus(List<ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean> resultList) {
        ll_result.setVisibility(View.GONE);
        rlv_service.setVisibility(VISIBLE);
        if(itemAdapter != null){
            itemAdapter.changeServiceItem(resultList);
        }

        switch (resultList.size()) {
            case 0:
                tv_new.setText(getString(R.string.x3_home_no_select));
                break;
            case 1:
                tv_new.setText(String.format(getString(R.string.x3_home_select_work),resultList.size()));
                break;
            default:
                tv_new.setText(String.format(getString(R.string.x3_home_select_works),resultList.size()));
                break;
        }

        PropertyValuesHolder show_alpha = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
        PropertyValuesHolder show_transY = PropertyValuesHolder.ofFloat("translationY", tv_new.getTranslationY(), 0.0f);
        showSet = ObjectAnimator.ofPropertyValuesHolder(tv_new, show_alpha, show_transY);
        showSet.setStartDelay(500);
        showSet.setDuration(500);

        PropertyValuesHolder hide_alpha = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
        hideSet = ObjectAnimator.ofPropertyValuesHolder(tv_new, hide_alpha);
        hideSet.setStartDelay(500);
        hideSet.setDuration(300);
        hideSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                tv_new.setVisibility(View.INVISIBLE);
                tv_new.setTranslationY(-X3ScreenUtils.dipToPx(30, X3AloneSearchActivity.this));
            }
        });

        showSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                hideSet.start();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                tv_new.setVisibility(VISIBLE);
            }
        });
        showSet.start();
    }

    /**
     * 搜索的本地记录
     */
    private void searchContent() {
        AloneSearchBean saveSearchBean = DaoSessionUtils.getInstance().selectAloneBean(mobile + "");
        if (saveSearchBean == null) {
            ll_result.setVisibility(View.GONE);
            return;
        }
        List<String> mList = saveSearchBean.getMList();
        if (X3StringUtils.isListEmpty(mList)) {
            ll_result.setVisibility(View.GONE);
            return;
        }
        if (saveAdapter != null) {
            saveAdapter.changeSearch(mList);
        }
    }

    @Override
    protected X3AloneServicePresenter createPresenter() {
        return new X3AloneServicePresenter();
    }

    @OnClick({R2.id.iv_back,R2.id.iv_clean,R2.id.tv_search,R2.id.tv_del})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_clean) {
            et_search.setText("");
            searchContent();
        } else if (id == R.id.tv_del) {
            DaoSessionUtils.getInstance().deleteAloneSearch();
            searchContent();
        } else if (id == R.id.tv_search) {/**
         * 搜索服务项目
         */
            String keyContent = et_search.getText().toString();
            if (X3StringUtils.isEmpty(keyContent)) {
                rlv_result.setVisibility(View.GONE);
                searchContent();
                return;
            }
            searchAloneCache();
            searchData(keyContent);
        }
    }

    /**
     * 将搜索的数据存储到本地数据库
     * @param keyContent
     */
    private void searchData(String keyContent) {
        AloneSearchBean saveSearchBean = DaoSessionUtils.getInstance().selectAloneBean(mobile + "");
        boolean isHaveSave = false;
        if (saveSearchBean == null) {
            saveSearchBean = new AloneSearchBean();
            saveSearchBean.setUserName(mobile);
            isHaveSave = true;
        }
        List<String> mList = saveSearchBean.getMList();
        if (X3StringUtils.isListEmpty(mList)) {
            mList = new ArrayList<>();
        }
        boolean isHave = false;
        for (String content : mList) {
            if (keyContent.equals(content)) {
                isHave = true;
            }
        }

        List<String> newList = new ArrayList<>(mList);
        if (!isHave) {
            if (newList.size() >= 10) {
                newList.remove(0);
            }
            newList.add(keyContent);
        }
        saveSearchBean.setMList(newList);

        if (isHaveSave) {
            DaoSessionUtils.getInstance().insertAloneSearch(saveSearchBean);
        } else {
            DaoSessionUtils.getInstance().updateAloneSearchBean(saveSearchBean);
        }
    }

    /**
     * 输入框获取焦点
     */
    protected void etGetFouce() {
        new Handler().postDelayed(() -> {
            if (et_search == null) {
                return;
            }
            et_search.setFocusable(true);
            et_search.setFocusableInTouchMode(true);
            et_search.requestFocus();
            InputMethodManager inputManager =
                    (InputMethodManager) et_search.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(et_search, 0);
        }, 1000);

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String result = editable.toString();
        iv_clean.setVisibility(X3StringUtils.isEmpty(result) ? View.GONE : View.VISIBLE);
        tv_search.setTextColor(X3StringUtils.isEmpty(result) ? Color.parseColor("#666666") : Color.parseColor("#0077ff"));
        tv_search.setClickable(!X3StringUtils.isEmpty(result));
        if (X3StringUtils.isEmpty(result)) {
            rlv_service.setVisibility(View.GONE);
            rlv_result.setVisibility(View.VISIBLE);
            ll_result.setVisibility(View.VISIBLE);
            searchContent();
        }
    }

    @Override
    public void iOnSaveClick(String selectText) {
        et_search.setText("");
        if (X3StringUtils.isEmpty(selectText)) {
            return;
        }
        et_search.setText(selectText + "");
        et_search.setSelection(selectText.length());

        searchAloneCache();
    }

    @Override
    public long providerWorkBaseId() {
        return workBaseId;
    }

    @Override
    public void getServiceInfo(List<ServiceHeaderBean> mList,boolean isJump) {

    }

    @Override
    public void getServiceList(List<AloneServiceListBean> mList,boolean isStart) {
        this.cacheList = mList;
    }

    @Override
    public void getUpdateServiceList(List<AloneServiceListBean> mList) {
        iOnPickerDismiss();
    }

    @Override
    public void iOnCuteService(boolean isCute, ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean serviceBean,int position) {
        serviceListBean = X3AloneServiceSearchUtils.searchAloneServiceBean(cacheList,serviceBean.getSn());

        if (isCute) {
            if (serviceListBean == null) {
                if (mPresenter != null) {
                    mPresenter.getDetectionServiceList(true);
                }
                return;
            }

            List<AloneServiceListBean.IdBean> idBeans = serviceListBean.getQuestionAndOptionIdVOList();

            if (!X3StringUtils.isListEmpty(idBeans)) {
                Iterator<AloneServiceListBean.IdBean> iterator = idBeans.iterator();
                while (iterator.hasNext()) {
                    AloneServiceListBean.IdBean next = iterator.next();
                    if (next.getOptionsId() == 0) {
                        iterator.remove();
                    }
                }
                List<Long> touchQuestionBaseIdList = new ArrayList<>();

                for (AloneServiceListBean.IdBean idBean : idBeans) {
                    touchQuestionBaseIdList.add(idBean.getQuestionBaseId());
                }

                serviceListBean.setTouchQuestionBaseIdList(touchQuestionBaseIdList);
                serviceListBean.setQuestionAndOptionIdVOList(idBeans);
            }
            deleteService(serviceListBean);
        }else {
            if (!needUpdateMile) {
                serviceListBean = addServiceItem(serviceBean, serviceListBean);
                iOnShowChangeServiceOne(serviceListBean, serviceListBean.getReductionType());
            }else {
                // 否则需要先确认总里程和日均里程
                mileDialog = new X3BottomAffirmMileDialog(this);
                mileDialog.show();
                mileDialog.setWorkBaseId(workBaseId,null);
                mileDialog.setiOnClickAffirmMiles((currentMiles1, perMiles1) -> {
                    // 确认完总里程和日均里程了
                    needUpdateMile = false;

                    newPerMiles = Double.parseDouble(X3StringUtils.formatDouble(perMiles1));
                    newCurrentMile = Double.parseDouble(X3StringUtils.formatDouble(currentMiles1));

                    CameraBean cameraBean = new CameraBean();
                    cameraBean.currentMiles = newCurrentMile;
                    cameraBean.perMiles = newPerMiles;
                    EventBus.getDefault().post(new CameraBean(X3Constent.CODE_ALONE_CHANGE_NEED_UPDATE,cameraBean));

                    serviceBean.setCurrentMiles(newCurrentMile);
                    serviceBean.setPerMiles(newPerMiles);

                    serviceListBean = addServiceItem(serviceBean, serviceListBean);

                    if (mPresenter != null) {
                        mPresenter.getDetectionServiceList(false);
                    }
                    if (itemAdapter != null) {
                        itemAdapter.refreshMiles(newCurrentMile, newPerMiles);
                    }

                    iOnShowChangeServiceOne(serviceListBean,serviceListBean.getReductionType());
                });
            }
        }
    }

    /**
     * 删除非系统推荐的服务
     * @param serviceListBean
     */
    private void deleteService(AloneServiceListBean serviceListBean) {
        List<Long> touchQuestionBaseIdList = serviceListBean.getTouchQuestionBaseIdList();
        if (!X3StringUtils.isListEmpty(touchQuestionBaseIdList) && touchQuestionBaseIdList.contains(questionBaseId)) {
            touchQuestionBaseIdList.remove(touchQuestionBaseIdList.indexOf(questionBaseId));
        }
        List<String> descriptionList = serviceListBean.getTouchQuestionBaseDescriptionList();
        if (!X3StringUtils.isListEmpty(descriptionList) && descriptionList.contains(decTitle)) {
            descriptionList.remove(descriptionList.indexOf(decTitle));
        }

        if (X3StringUtils.isListEmpty(touchQuestionBaseIdList)) {
            Iterator<AloneServiceListBean> iterator = cacheList.iterator();
            while (iterator.hasNext()) {
                AloneServiceListBean next = iterator.next();
                if (next.getTouchServiceSn().equals(serviceListBean.getTouchServiceSn())) {
                    iterator.remove();
                }
            }
        }else {
            serviceListBean.setTouchQuestionBaseDescriptionList(descriptionList);
            serviceListBean.setTouchQuestionBaseIdList(touchQuestionBaseIdList);
            cacheList = X3AloneServiceSearchUtils.replaceAloneServiceBean(cacheList,serviceListBean.getTouchServiceSn(),serviceListBean);
        }
        if (mPresenter != null) {
            mPresenter.saveDetectionService(cacheList);
        }
    }

    public void iOnShowChangeServiceOne(AloneServiceListBean changeServiceBean,String remindType) {
        this.changeServiceBean = changeServiceBean;
        modifyMileOrTime(changeServiceBean,remindType);
    }

    private AloneServiceListBean addServiceItem(ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean serviceBean,AloneServiceListBean serviceListBean1) {
        List<Long> idList = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        List<AloneServiceListBean.IdBean> idBeans = new ArrayList<>();
        AloneServiceListBean newServiceBean = serviceListBean1;
        if (newServiceBean == null) {
            newServiceBean = new AloneServiceListBean();
            newServiceBean.setWorkBaseId(workBaseId);
            newServiceBean.setRecommendSafeMiles(0);
            newServiceBean.setTouchServiceBaseName(serviceBean.getName());
            newServiceBean.setTouchServiceSn(serviceBean.getSn());
            newServiceBean.setCurrentMiles(serviceBean.getCurrentMiles());
            newServiceBean.setPerMiles(serviceBean.getPerMiles());
            if (!idList.contains(questionBaseId)) {
                idList.add(questionBaseId);
            }
            titleList.add(decTitle);
            idBeans.add(new AloneServiceListBean.IdBean(questionBaseId,0));
            newServiceBean.setSystemList(new ArrayList<>());
            newServiceBean.setTouchQuestionBaseDescriptionList(titleList);
            newServiceBean.setTouchQuestionBaseIdList(idList);
            newServiceBean.setQuestionAndOptionIdVOList(idBeans);

            cacheList.add(newServiceBean);
        }else {
            idList = newServiceBean.getTouchQuestionBaseIdList();
            titleList = newServiceBean.getTouchQuestionBaseDescriptionList();
            idBeans = newServiceBean.getQuestionAndOptionIdVOList();

            if (X3StringUtils.isListEmpty(idBeans)) {
                idBeans = new ArrayList<>();
            }
            if (X3StringUtils.isListEmpty(idList)) {
                idList = new ArrayList<>();
            }
            if (X3StringUtils.isListEmpty(titleList)) {
                titleList = new ArrayList<>();
            }
            if (!idList.contains(questionBaseId)) {
                idList.add(questionBaseId);
            }
            if (!titleList.contains(decTitle)) {
                titleList.add(decTitle);
            }
            boolean isHave = false;
            for (int i = 0; i < idBeans.size(); i++) {
                if (idBeans.get(i).getQuestionBaseId() == questionBaseId) {
                    isHave = true;
                    break;
                }
            }
            if (!isHave) {
                idBeans.add(new AloneServiceListBean.IdBean(questionBaseId,0));
            }

            newServiceBean.setTouchQuestionBaseDescriptionList(titleList);
            newServiceBean.setTouchQuestionBaseIdList(idList);
            cacheList = X3AloneServiceSearchUtils.replaceAloneServiceBean(cacheList,newServiceBean.getTouchServiceSn(),newServiceBean);
        }
        return newServiceBean;
    }

    @Override
    public void iOnShowChangeService(String sn,String remindType,double currentMiles,double perMiles) {
        changeServiceBean = X3AloneServiceSearchUtils.searchAloneServiceBean(cacheList,sn);
        changeServiceBean.setCurrentMiles(currentMiles);
        changeServiceBean.setPerMiles(perMiles);

        modifyMileOrTime(changeServiceBean,remindType);
    }

    private void modifyMileOrTime(AloneServiceListBean changeServiceBean,String remindType) {
        if (changeServiceBean.getCurrentMiles() <= 0 && changeServiceBean.getPerMiles() <= 1) {
            changeServiceBean.setPerMiles(newPerMiles);
            changeServiceBean.setCurrentMiles(newCurrentMile);
        }
        mileOrTimeDialog = new X3NewBottomMileOrTimeDialog(this,1);
        mileOrTimeDialog.show();
        mileOrTimeDialog.showCurrentMile(changeServiceBean,remindType);
        mileOrTimeDialog.setiOnAffirmSelect(this);
    }

    @Override
    public void iOnShowDelete(int position, String sn) {
        AloneServiceListBean serviceListBean = X3AloneServiceSearchUtils.searchAloneServiceBean(cacheList,sn);
        if (serviceListBean == null) {
            return;
        }

        List<String> touchIdList = serviceListBean.getTouchQuestionBaseDescriptionList();
        if (X3StringUtils.isListEmpty(touchIdList)) {
            touchIdList = new ArrayList<>();
        }

        List<AloneServiceListBean.IdBean> idBeans = serviceListBean.getQuestionAndOptionIdVOList();

        if (touchIdList.size() - 1 <= 0) {
            delDialog = new X3PointDialog(this,"",getString(R.string.x3_delete_add_service),getString(R.string.x3_cancel_info),getString(R.string.x3_affirm_info));
            delDialog.show();
            delDialog.setRightClick(view -> {
                delDialog.dismiss();
                if (!X3StringUtils.isListEmpty(idBeans) && idBeans.size() > 0) {
                    AloneServiceListBean.IdBean idBean = idBeans.get(0);
                    if (idBean.getOptionsId() != 0) {
                        X3ToastUtils.showMessage(getString(R.string.no_can_delete_item));
                    }else {
                        if (itemAdapter != null) {
                            itemAdapter.deleteService(position,true,true);
                        }
                    }
                }else {
                    if (itemAdapter != null) {
                        itemAdapter.deleteService(position,true,true);
                    }
                }
            });
        }else {
            if (touchIdList.contains(decTitle)) {
                touchIdList.remove(touchIdList.indexOf(decTitle));
            }
            String serviceName = "";
            for (int i = 0; i < touchIdList.size(); i++) {
                if (i == 0) {
                    serviceName = touchIdList.get(i);
                }else {
                    serviceName = serviceName + "," + touchIdList.get(i);
                }
            }
            delDialog = new X3PointDialog(this,"",String.format(getString(R.string.x3_delete_all_add_service),serviceName),getString(R.string.x3_no_tips),getString(R.string.x3_yes_tips));
            delDialog.show();
            delDialog.setLeftClick(view -> {
                delDialog.dismiss();

                if (!X3StringUtils.isListEmpty(idBeans)) {
                    boolean isSystem = false;
                    for (int i = 0; i < idBeans.size(); i++) {
                        if (idBeans.get(i).getQuestionBaseId() == questionBaseId && idBeans.get(i).getOptionsId() == questionOptionId) {
                            isSystem = true;
                            break;
                        }
                    }
                    if (isSystem) {
                        X3ToastUtils.showMessage(getString(R.string.no_can_delete_item));
                    }else {
                        if (itemAdapter != null) {
                            itemAdapter.deleteService(position,true,false);
                        }
                    }

                }else {
                    if (itemAdapter != null) {
                        itemAdapter.deleteService(position,true,false);
                    }
                }
            });
            delDialog.setRightClick(view -> {
                delDialog.dismiss();
                Iterator<AloneServiceListBean.IdBean> iterator = idBeans.iterator();
                while (iterator.hasNext()) {
                    AloneServiceListBean.IdBean next = iterator.next();
                    if (next.getOptionsId() == 0) {
                        iterator.remove();
                    }
                }
                serviceListBean.setQuestionAndOptionIdVOList(idBeans);
                X3AloneServiceSearchUtils.replaceAloneServiceBean(cacheList,sn,serviceListBean);

                iOnPickerDismiss();
                if (mPresenter != null) {
                    mPresenter.saveDetectionService(cacheList);
                }
                if (itemAdapter != null) {
                    itemAdapter.deleteAllService(position);
                }
            });
        }
    }

    @Override
    public void iOnAffirmMiles(AloneServiceListBean listBean) {
        boolean isHave = false;
        newCacheList = cacheList;
        for (int i = 0; i < newCacheList.size(); i++) {
            AloneServiceListBean aloneServiceListBean = newCacheList.get(i);
            if (aloneServiceListBean.getTouchServiceSn().equals(listBean.getTouchServiceSn())) {
                isHave = true;
                newCacheList.remove(i);
                newCacheList.add(i,listBean);
                break;
            }
        }
        if (!isHave) {
            newCacheList.add(listBean);
        }
        CameraBean cameraBean = new CameraBean(X3Constent.CODE_ALONE_ADD_SERVICE_REQUEST,newCacheList);
        EventBus.getDefault().post(cameraBean);
        mPresenter.saveDetectionService(newCacheList);
        if (itemAdapter != null) {
            itemAdapter.changeMilesService(changeServiceBean);
        }
    }

    @Override
    public void iOnPickerDismiss() {
        CameraBean cameraBean = new CameraBean(X3Constent.CODE_ALONE_ADD_SERVICE_REQUEST,cacheList);
        EventBus.getDefault().post(cameraBean);
    }


    @Override
    protected void onDestroy() {
        if (showSet != null) {
            showSet.cancel();
        }
        if (hideSet != null) {
            hideSet.cancel();
        }
        super.onDestroy();
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            String keyContent = et_search.getText().toString();
            if (X3StringUtils.isEmpty(keyContent)) {
                return true;
            }

            tv_search.performClick();
        }
        return true;
    }

    public void aloneRefresh(){
        CameraBean cameraBean = new CameraBean(X3Constent.CODE_ALONE_ADD_SERVICE_REFRESH,1);
        EventBus.getDefault().post(cameraBean);
    }
}